package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/21
 */

import com.qf.domain.User;
import com.qf.service.AdminUserService;
import com.qf.service.impl.AdminUserServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/21 9:23
 * description:
 */
@WebServlet("/adminLogin")
public class AdminUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            response.sendRedirect("admin/login.jsp");
            return;
        }

        try {
            AdminUserService adminUSerservice = new AdminUserServiceImpl();
            User admin =adminUSerservice.login(username,password);
            if(admin!=null){
                request.getSession().setAttribute("admin", admin);
                request.getRequestDispatcher("admin/admin.jsp").forward(request, response);
            }else{
                response.sendRedirect("admin/login.jsp");
            }
        } catch (Exception e) {
            response.sendRedirect("error/404.html");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}